﻿using System;
using System.Collections.Generic;
using CodeFirstMVC3.Core;
using CodeFirstMVC3.DataAccess.Model;
using CodeFirstMVC3.Domain.DataAccess;
using CodeFirstMVC3.Domain.Model;
using CodeFirstMVC3.Domain.Service;

namespace CodeFirstMVC3.Service
{
    public class JobService : IJobService
    {
        private readonly IRepository<Job> _repository;

        public JobService(IRepository<Job> repository)
        {
            _repository = repository;
        }

        public IJob New()
        {
            return _repository.NewEntity();
        }

        public void Add(IJob entity)
        {
            _repository.Add(entity as Job);
            _repository.Save();
        }

        public void Edit(IJob entity)
        {
            Job m = entity as Job;
            _repository.Save();
        }

        public void Remove(IJob entity)
        {
            _repository.Remove(entity as Job);
            _repository.Save();
        }

        public IJob Get(int id)
        {
            return _repository.Get(p => p.Id == id);
        }

        public IJob Get(Func<IJob, bool> predicate)
        {
            return _repository.Get(predicate);
        }

        public int GetCount(Func<IJob, bool> perdicate = null)
        {
            if (perdicate == null)
                return _repository.GetCount(p => true);
            return _repository.GetCount(perdicate);
        }

        public IEnumerable<IJob> GetAll(Func<IJob, bool> predicate = null, int start = 0, int count = ApplicationSettings.PageSize)
        {
            if (predicate == null)
                return _repository.GetAll(p => true);

            if (start == 0)
            {
                return _repository.GetAll(predicate);
            }

            return _repository.GetAll(predicate, start, count);
        }
    }
}
